<?php
/*

*/
class System_App_Database extends System_App_Noyau{
	private static $connection = null;
        
        function __construct(){
            $this->register('database');
            $src = APP_PATH.'config/db.ini';
            if( file_exists($src) ){
                $datas = parse_ini_file($src, true);
                
                define( 'DB_HOST', $datas['database'][ 'host' ] );
                define( 'DB_NAME', $datas['database'][ 'dbname' ] );
                define( 'DB_LOGIN', $datas['database'][ 'login' ] );
                define( 'DB_MDP', $datas['database'][ 'mdp' ] );
            }
        }
        
	public function getConnection(){
            if( self::$connection === null ){
                try{
                    self::$connection = mysql_connect( DB_HOST, DB_LOGIN, DB_MDP);
                    mysql_select_db( DB_NAME, self::$connection );
                    return true;
                }catch (Exception $error_string){
                    return false;
                }
            }else{
                return true;
            }
	}
	
	public function getQueryResult( $query, $single = false ){
            if( $this->getConnection() ){
                if( $sql_result = $this->sqlResultToArray( mysql_query( $query ) ) ){
                    return ( $single )?$sql_result[0]:$sql_result;
                }else{
                    return array();
                }
            }else{
                return false;
            }
	}
	
	public function sqlResultToArray( $sql_results ){
            $aReturn = array();
            while( $result = @mysql_fetch_assoc( $sql_results ) ){
                    $aReturn[] = $result;
            }
            return $aReturn;
	}
}
?>